//
//  LoginVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/15.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "LoginVC.h"
#import <UIColor+Wonderful.h>
#import "RegisterVC.h"
@interface LoginVC () <UITextFieldDelegate>
@property (nonatomic, retain) UIImageView *backImg;
@property (nonatomic, retain) UITextField *usernameText;
@property (nonatomic, retain) UITextField *passwordText;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *registerButton;

@end

@implementation LoginVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //左侧返回按钮
    self.navigationItem.title = @"登录";
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back)] autorelease];
    self.navigationController.navigationBar.tintColor = [UIColor peachRed];
    
    // 创建视图
    [self createSubviews];
    
}

/// 创建视图方法
- (void)createSubviews
{
    // backImg
    self.backImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImg.image = [UIImage imageNamed:@"loginBackground.jpg"];
    [self.view addSubview:_backImg];
    [_backImg release];
    
    //userText
    self.usernameText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 15)];
    _usernameText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 2 + 4);
    _usernameText.placeholder = @"请输入用户名";
    _usernameText.clearButtonMode = YES;
    _usernameText.clearsOnBeginEditing = YES;
    _usernameText.borderStyle = UITextBorderStyleRoundedRect;
    _usernameText.delegate = self;
    _usernameText.tag = 1000;
    [self.view addSubview:_usernameText];
    [_usernameText release];
    
    //passwordText
    self.passwordText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _usernameText.frame.size.width, _usernameText.frame.size.height)];
    _passwordText.center = CGPointMake(_usernameText.center.x, _usernameText.center.y + _usernameText.frame.size.height + 20);
    _passwordText.placeholder = @"请输入密码";
    _passwordText.clearButtonMode = YES;
    _passwordText.clearsOnBeginEditing = YES;
    _passwordText.borderStyle = UITextBorderStyleRoundedRect;
    _passwordText.delegate = self;
    _passwordText.tag = 2000;
    _passwordText.secureTextEntry = YES;
    [self.view addSubview:_passwordText];
    [_passwordText release];
    
    //loginButton
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //
    _loginButton.frame = CGRectMake(_usernameText.frame.origin.x, _passwordText.frame.origin.y + _passwordText.frame.size.height + 20, (_usernameText.frame.size.width - 30) / 2, self.view.frame.size.height / 19);
    _loginButton.backgroundColor = [UIColor aquamarine];
    _loginButton.layer.cornerRadius = 5;
    _loginButton.tintColor = [UIColor peachRed];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:_loginButton];
    
    //register
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.frame = CGRectMake(_loginButton.frame.origin.x + _loginButton.frame.size.width + 30, _loginButton.frame.origin.y, _loginButton.frame.size.width, _loginButton.frame.size.height);
    _registerButton.backgroundColor = [UIColor aquamarine];
    _registerButton.layer.cornerRadius = 5;
    _registerButton.tintColor = [UIColor peachRed];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    //注册按钮方法
    [_registerButton addTarget:self action:@selector(registerPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
}

///回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:2000] becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}

/// 当输入框太靠下,遮住的时候编辑开始让输入框上移
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 2000) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 50);
    }
    return YES;
}

///编辑结束的时候让输入框移动回来
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField.tag == 2000) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 50);
    }
    return YES;
}

/// 注册按钮方法实现(跳转注册页面)
- (void)registerPage
{
    NSLog(@"11111");
    RegisterVC *registerVC = [[RegisterVC alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 左侧按钮实现
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
